package com.dxx.cloud.admin.controller.admin;

import com.dxx.cloud.admin.constant.CommonConst;
import com.dxx.cloud.admin.enums.CacheKeyEnums;
import com.dxx.cloud.admin.module.entity.CustomerCart;
import com.dxx.cloud.admin.module.entity.GoodsSizeEntity;
import com.dxx.cloud.admin.module.request.GoodsSizeCommentRequest;
import com.dxx.cloud.admin.module.request.IdRequest;
import com.dxx.cloud.admin.module.response.CustomerCartResponse;
import com.dxx.cloud.admin.module.response.GoodsSizeResponse;
import com.dxx.cloud.admin.service.IGoodsSizeService;
import com.dxx.cloud.admin.service.ISysConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.redis.utils.RedisUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping(CommonConst.RouterPath.ADMIN + "/goodsSize")
@Tag(name = "商品规格模块")
@Slf4j
public class GoodsSizeController {

    @Resource
    private ISysConfigService sysConfigService;

    @Resource
    private IGoodsSizeService goodsSizeService;

    @Operation(summary = "推荐列表")
    @RequestMapping(value = "/commentList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R<List<GoodsSizeResponse>> getCommentList(@RequestBody @Validated GoodsSizeCommentRequest request)  {
        String cacheKey = CacheKeyEnums.GOODS_SIZE_COMMENT_LIST.toKey(request.getName());
        List<GoodsSizeResponse> ret = RedisUtils.getCacheObject(cacheKey);
        if(null == ret) {
            String goodsSizeComment = sysConfigService.getValue(request.getType(), request.getName());
            if (StringUtils.hasText(goodsSizeComment)) {
                List<Long> list = Arrays.stream(goodsSizeComment.split(",")).map(Long::valueOf).toList();
                if(!CollectionUtils.isEmpty(list)) {
                    ret = goodsSizeService.listResponseByIds(list);
                }
            }
        }
        if(null != ret) {
            RedisUtils.setCacheObject(cacheKey, ret);
        }
        return R.ok(ret);
    }


    @Operation(summary = "上下架切换")
    @RequestMapping(value = "/onSellSwitch", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R<Boolean> onSellSwitch(@RequestBody @Validated GoodsSizeEntity request)  {
        return R.ok(goodsSizeService.onSellSwitch(request));
    }
}
